<template>
  <div class="container">
    <h1 class="title">用户信息</h1>
    <div class="table-container">
      <vxe-table :data="tableUsers" :row-class-name="highlightRow" :row-config="{ isCurrent: true }"
        @current-change="selectRow" border :column-config="{ resizable: true }" :scroll-y="{ enabled: true } ">
        <vxe-column type="seq" width="70"></vxe-column>
        <vxe-column field="id" title="ID"></vxe-column>
        <vxe-column field="name" title="姓名"></vxe-column>
        <vxe-column field="age" title="年龄"></vxe-column>
        <vxe-column field="email" title="邮件"></vxe-column>
      </vxe-table>
    </div>
    <div class="button-container">
      <button @click="fetchUsers">查询</button>
      <button @click="openNewUserDialog">新建</button>
      <button :disabled="!selectedRow" @click="openModifyUserDialog">修改</button>
      <button :disabled="!selectedRow" @click="deleteUser">删除</button>
    </div>
    <!-- 新建用户对话框 -->
    <NewUserDialog ref="newUserDialog" @userCreated="fetchUsers" />
    <ModifyUserDialog ref="modifyUserDialog" @userModified="fetchUsers" />
  </div>
</template>
 
<script setup>
import { deleteUserById, getAllUsers } from '@/alova/api';
import ModifyUserDialog from '@/components/ModifyUserDialog.vue';
import NewUserDialog from '@/components/NewUserDialog.vue';
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus';
import { ref } from 'vue';

const tableUsers = ref([]);
const loading = ref(false); 
const selectedRow = ref(null);

const fetchUsers = async () => {
  loading.value = true; // 开始加载
  const loader = ElLoading.service({ 
    fullscreen: true,
    text: "数据加载中...",
  });
  getAllUsers().then(data => {
    loading.value = false;
    tableUsers.value = data;
  }).catch(error => {
    console.error("查询用户失败:", error);
  }).finally(() => {
    loading.value = false; // 结束加载
    loader.close(); // 关闭加载动画
    selectedRow.value = null;
  });
};

const deleteUser = () => {
  ElMessageBox.confirm(
    '此操作将永久删除该用户，是否继续？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    deleteUserById(selectedRow.value.id).then(()=>{
      ElMessage.success('删除成功！');
      fetchUsers();
    }).catch(error => {
        console.log("删除用户失败:",error);
    }).finally(() => {
        console.log("finally");
    });
  }).catch(() => {
    ElMessage.info('已取消删除');
  });
};


const selectRow = ({ row }) => {
  selectedRow.value = row;
};
const highlightRow = ({ row }) => {
  return 'hover-row';
};

const newUserDialog = ref(null);
const modifyUserDialog = ref(null);
// 打开对话框
const openNewUserDialog = () => {
    newUserDialog.value.openDialog();
};
const openModifyUserDialog = () => {
  modifyUserDialog.value.openDialog(selectedRow.value);
};
</script>

<style scoped>
.container {
  display: flex;
  flex-direction: column;
   /* 让整个布局填满可用高度 height: 100vh;*/
   width: 100%;
}

.title {
  text-align: center;
  font-size: 24px;
  margin-bottom: 20px;
}

.table-container {
  flex: 1; /* 让表格区域填满剩余空间 */
  display: flex;
  flex-direction: row;
  width: 100%;
  overflow: auto; /* 允许表格滚动 */
}

.vxe-table {
  flex: 1; /* 让表格占满容器 */
  width: 100%; /* 让表格水平铺满 */
}

.button-container {
  display: flex;
  justify-content: flex-end; /* 按钮靠右对齐 */
  gap: 10px; /* 按钮之间的间距 */
  margin-top: 10px;
}

.selected-row {
  background-color: #0d51e2 !important;
}

.hover-row:hover {
  background-color: #f0f8ff !important;
}
</style>
